#include "deleteuserwidget.h"

DeleteUserWidget::DeleteUserWidget(QWidget *parent) : QWidget(parent)
{
           init_Widget();
           init_Control();
           init_Connect();
}
void DeleteUserWidget::init_Control()
{
    this->font.setPointSize(10);
    this->setFont(this->font);

    this->informationEt=new QLineEdit();


    this->informationLab=new QLabel();
    this->informationLab->setText(QString::fromUtf8("请输入删除用户的id："));

    this->confirmBtn=new QPushButton();
    this->confirmBtn->setText(QString::fromUtf8("确认"));
    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));

    this->btnhLay=new QHBoxLayout();
    this->btnhLay->addWidget(confirmBtn);
    this->btnhLay->addWidget(returnBtn);

    this->otherhLay=new QHBoxLayout();
    this->otherhLay->addWidget(informationLab);
    this->otherhLay->addWidget(informationEt);

    this->vLay=new QVBoxLayout();
    this->vLay->addLayout(otherhLay);
    this->vLay->addLayout(btnhLay);

    this->vLay->setAlignment(Qt::AlignCenter);

    this->setLayout(this->vLay);
}

void DeleteUserWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("删除用户"));
    this->resize(800,600);
}

void DeleteUserWidget::init_Connect()
{
    connect(this->confirmBtn,SIGNAL(clicked()),this,SLOT(on_confirmBtn_cliked()));
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
}

int DeleteUserWidget::deleteUser()
{
    QString id;user=nullptr;
    id=this->informationEt->text();
    QString sql=QString("SELECT *FROM Users WHERE user_id='%1';").arg(id);
    this->model=this->control->getInstance()->Creatmodel("user");
    this->user=this->model->getUserByName((char*)sql.toStdString().c_str());
    if(user)
    {
        sql=QString("DELETE FROM Users WHERE user_id='%1';").arg(id);
        this->model->Table_Delete((char*)sql.toStdString().c_str());
        QMessageBox::information(this,"成功","删除成功");
        this->informationEt->clear();
    }
    else
    {
        QMessageBox::critical(this, "错误", "没有该用户");
        return 0;
    }
    return 1;
}

void DeleteUserWidget::on_retutrnBtn_cliked()
{
     emit returnManager();
}

void DeleteUserWidget::on_confirmBtn_cliked()
{
    if(deleteUser())
    {
       emit returnManager();
    }
    else
    {
        this->informationEt->clear();
    }

}
